﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using XcoAppSpaces.Core;
using XcoAppSpaces.Contracts.Exceptions;

namespace test.XcoAppSpaces.Core
{
	[TestFixture]
	public class testSocketAuthentication
	{
		[Test]
		public void CommunicationWithAuthenticatedSockets()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001;tcp.key=secure123"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure123"))
			{
				server.Run<string>(msg => { });

				Assert.That(() => client.Connect<string>("localhost:8002"), Throws.Nothing);
			}
		}

		[Test]
		public void CannotConnectWithInvalidKey()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001;tcp.key=secure123"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure456"))
			{
				server.Run<string>(msg => { });
				Assert.That(() => client.Connect<string>("localhost:8002"), Throws.InstanceOf<XcoCommunicationException>());
			}
		}

		[Test]
		public void CannotConnectWithoutKey()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure123"))
			{
				server.Run<string>(msg => { });
				Assert.That(() => client.Connect<string>("localhost:8002"), Throws.InstanceOf<XcoCommunicationException>());
			}
		}
	}
}
